<?php

namespace App\Http\Services;

class Rank
{
    private  $redis;

    private $key;

    public function __construct($key)
    {

        $this->key = $key;
        $this->redis = new \Redis();
        $this->redis->connect('127.0.0.1');
    }


    public function updateScore($uid, $score=null)
    {
        $this->redis->zAdd($this->key,$score,$uid);
    }


    public function IncrScore($uid, $score=1)
    {
        $this->redis->zIncrBy($this->key,$score,$uid);
    }


    public function getScore($uid)
    {
       return  $this->redis->zScore($this->key,$uid);
    }


    public function getList($topN)
    {
        return $this->redis->zRevRange($this->key, 0, $topN);
    }

    public function getRank($uid)
    {
        return $this->redis->zRevRank($this->key,$uid)+1;
    }


    public function del($uid)
    {
       return $this->redis->zRem($this->key, $uid);
    }


    public function flush()
    {
        $this->redis->del($this->key);
    }

    public static function __callStatic($funName,$args)
    {

        dump($funName,$args);
    }

    public function __destruct()
    {
        dump('触发把');
        $this->redis->close();
    }
}
